package com.huadao.cart_ccs.experiment.log.baseLoglist;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.base.BaseListFragment;
import com.huadao.cart_ccs.base.BasePresenter;
import com.huadao.cart_ccs.experiment.log.OnDay;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseLogListFrag extends BaseListFragment implements OnDay,IBaseLogListContract.IBaseLogListView,OnCondition {

    private BaseLogListPresenter baseLogListPresenter;
    protected List dataList;

    @Override
    public int getRecycleViewId() {
        return R.id.recycleView;
    }

    @Override
    public int getSwipeRefreshLayoutId() {
        return R.id.swipeRefreshLayout;
    }

    @Override
    protected void init() {
        baseLogListPresenter=createPresenter();
        Bundle bundle=getArguments();
        if(bundle!=null&&bundle.containsKey("day")){
            int day=bundle.getInt("day");
            baseLogListPresenter.setDay(day);
            String condition=bundle.getString("condition");
            baseLogListPresenter.setCondition(condition);
        }
        setEnableLoadMore(false);
        dataList=new ArrayList<>();
    }

    public void setDayConditionBundle(int day,String condition){
        Bundle bundle=new Bundle();
        bundle.putInt("day",day);
        bundle.putString("condition",condition);
        this.setArguments(bundle);
    }

    protected abstract BaseLogListPresenter createPresenter();

    @Override
    protected RecyclerView.ItemDecoration setItemDecoration() {
        return new DividerItemDecoration(this.getContext(), DividerItemDecoration.VERTICAL);
    }

    @Override
    public RecyclerView.LayoutManager setLayoutManage() {
        return new LinearLayoutManager(this.getContext());
    }

    @Override
    protected void loadData() {
        baseLogListPresenter.getDataList();
    }

    @Override
    protected void loadMoreData() {
        baseLogListPresenter.getMoreData();
    }

    @Override
    public int getContentViewId() {
        return R.layout.frag_list;
    }

    @Override
    protected BasePresenter getPresenter() {
        return baseLogListPresenter;
    }

    @Override
    public void daySelected(int day) {
        if(baseLogListPresenter!=null&&day!=baseLogListPresenter.getDay()) {
            baseLogListPresenter.setDay(day);
            onRefresh();
        }
    }

    @Override
    public void conditionSelected(String condition) {
        if(baseLogListPresenter!=null&&condition!=baseLogListPresenter.getCondition()) {
            baseLogListPresenter.setCondition(condition);
            onRefresh();
        }
    }

    public void showDataList(List dataList){
        this.dataList.clear();
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
        setSwipeRefresh(false);
    }

    public void showMoreDataList(List dataList){
        if(dataList==null||dataList.size()==0){
            showToast("没有更多数据");
            return;
        }
        int start=this.dataList.size();
        this.dataList.addAll(dataList);
        customeAdapter.notifyItemRangeInserted(start,dataList.size());
    }
}
